package com.nowcoder.code.hjnc;

import java.util.Arrays;
import java.util.HashMap;

public class NC95 {

    public int MLS(int[] arr) {

        Arrays.sort(arr);
        int max = 1;
        int li = max;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == arr[i - 1] + 1) {
                li++;
                max = Math.max(li, max);
            } else if (arr[i] != arr[i - 1]) {
                li = 1;
            }

        }
        return max;
    }

    public int longestConsecutive(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int max = 0;
        for (int i : nums) {
            Integer li = map.get(i);
            if (li != null) {
                continue;
            }
            li = 1;
            Integer left = map.get(i - 1);
            Integer right = map.get(i + 1);

            if (left != null) {
                li = li + left;
            }
            if (right != null) {
                li = li + right;
            }
            if (left != null) {
                map.put(i - left, li);
            }
            if (right != null) {
                map.put(i + right, li);
            }
            map.put(i, li);
            max = Math.max(max, li);
        }
        return max;
    }

    public static void main(String[] args) {
        int[] is = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1};
        System.out.println(new NC95().longestConsecutive(is));

    }

}
